//set working directory 
cd  "C:\Users\johna\Dropbox (Curiel Analytx)\ElectionNightResults\replication_code"

//read in the state dta file 
use "data\nyt_states.dta", clear
sort state time
drop total // Don't need this.  We'll generate two-party total later


** calculate reporting momentum

gen total4 = norm_total if hoursfromclose <= 4 & hoursfromclose[_n+1] > 4
gen total8 = norm_total if hoursfromclose <= 8 & hoursfromclose[_n+1] > 8
gen total24 = norm_total if hoursfromclose <= 24 & hoursfromclose[_n+1] > 24
gen total48 = norm_total if hoursfromclose <= 48 & hoursfromclose[_n+1] > 48

egen maxtotal4 = max(total4),by(state)
egen maxtotal8 = max(total8),by(state)
egen maxtotal24 = max(total24),by(state)
egen maxtotal48 = max(total48),by(state)

preserve // for the velocity graphs
keep if state ~= state[_n+1]
replace maxtotal4 = 94.41113 if state == "hawaii"




** Show relationship between count velocity and pre-processing

gen nocounting = inlist(state,"alabama","connecticut","district-of-columbia","idaho","kentucky","maryland","massachusetts","michigan","mississippi") == 1
replace nocounting = 1 if inlist(state,"new-hampshire","new-york","pennsylvania","south-carolina","south-dakota","west-virginia","wisconsin","wyoming") == 1
replace nocounting = 0 if nocounting == .

table nocounting,c(mean maxtotal4)
sort maxtotal4
gen order4 = _n

gen state_lbl = proper(subinstr(state,"-"," ",.))
replace state_lbl = "D.C." if state_lbl == "District Of Columbia"

twoway (scatter order4 maxtotal4 if nocounting == 1, mcolor(red) msymbol(circle) mlabel(state_lbl) mlabsize(tiny) mlabposition(9)) ///
       (scatter order4 maxtotal4 if nocounting == 0, mcolor(green*1.2) msymbol(circle) mlabel(state_lbl) mlabsize(tiny) mlabposition(9)) ///
	   , ///
	   ytitle("") ///
	   ylabel(none) ///
	   xtitle(" ") ///
	   xline(77.84148 63.05615, lpattern(shortdash)) ///
	   title(Progress after 4 hours) ///
	   legend(order(2 "Preprocessing allowed" 1 "Preprocessing prohibited") ///
	   size(vsmall)) xsize(4) ysize(6) 
graph export "figures\fig10a.png", replace width(2500)

table nocounting,c(mean maxtotal8)
sort maxtotal8
gen order8 = _n
//getting rid of processing options
twoway (scatter order8 maxtotal8 if nocounting == 1, mcolor(red) msymbol(circle) mlabel(state_lbl) mlabsize(tiny) mlabposition(9)) ///
       (scatter order8 maxtotal8 if nocounting == 0, mcolor(green*1.2) msymbol(circle) mlabel(state_lbl) mlabsize(tiny) mlabposition(9)) ///
	   , ///
	   ytitle("") ///
	   ylabel(none) ///
	   xtitle(" ") ///
	   xline(88.55422 79.19339, lpattern(shortdash)) ///
	   title(Progress after 8 hours) ///
	   legend(order(2 "Preprocessing allowed" 1 "Preprocessing prohibited") ///
	   size(vsmall)) xsize(4) ysize(6) 	   
graph export "figures\preprocessing10b.png", replace width(2500)


table nocounting,c(mean maxtotal24)
sort maxtotal24
gen order24 = _n
twoway (scatter order24 maxtotal24 if nocounting == 1, mcolor(red) msymbol(circle) mlabel(state_lbl) mlabsize(tiny) mlabposition(9)) ///
       (scatter order24 maxtotal24 if nocounting == 0, mcolor(green*1.2) msymbol(circle) mlabel(state_lbl) mlabsize(tiny) mlabposition(9)) ///
	   , ///
	   ytitle("") ///
	   ylabel(none) ///
	   xtitle(" ") ///
	   xscale(range(0 100)) ///
	   xlabel(0(20)100) ///
	   xline(91.95538 90.36799, lpattern(shortdash)) ///
	   title(Progress after 24 hours) ///
	   legend(order(2 "Preprocessing allowed" 1 "Preprocessing prohibited") ///
	   size(vsmall)) xsize(4) ysize(6)   
graph export "figures\preprocessing10c.png", replace width(2500)
